﻿
//Author: 张亚涛
//createtime:2015-12-15 23:09:25
//Description: 

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NS.BLL.Systems;

namespace BLL
{

    /// <summary>
    /// 该类提供了一系列操作 "员工管理" 的方法;
    /// </summary>
    public class UserService
    {
        /// <summary>
        /// 获取员工工号
        /// </summary>
        /// <param name="WeChat">员工微信号</param>
        /// <returns></returns>        
        public static string GetUserID(string WeChat)
        {
            string res = "";
            object[] obj = UtilityService.GetFieldValueByPK("UserDetail", "UserID", "WeChat", WeChat);
            if (obj != null && obj[0] != null)
            {
                res = obj[0].ToString();
            }
            if (!string.IsNullOrEmpty(res))
            {
                res = res.PadLeft(4, '0');
            }
            return res;
        }

        /// <summary>
        /// 获取员工微信号
        /// </summary>
        /// <param name="WeChat">员工ID</param>
        /// <returns></returns>        
        public static string GetWeChat(long UserID)
        {
            string res = string.Empty;
            object[] obj = UtilityService.GetFieldValueByPK("UserDetail", "WeChat", "UserID", UserID.ToString());
            if (obj != null && obj[0] != null)
            {
                res = obj[0].ToString();
            }
            return res;
        }

        /// <summary>
        /// 根据员工微信号获得用户姓名
        /// </summary>
        /// <param name="WeChat">员工微信号</param>
        /// <returns></returns>        
        public static string GetTrueName(string WeChat)
        {
            string res = "";
            object[] obj = UtilityService.GetFieldValueByPK("UserDetail", "TrueName", "WeChat", WeChat);
            if (obj != null && obj[0] != null)
            {
                res = obj[0].ToString();
            }
            return res;
        }

        /// <summary>
        /// 员工微信号是否激活  
        /// </summary>
        /// <param name="WeChat">员工微信号</param>
        /// <returns></returns>        
        public static bool IsActivate(string WeChat)
        {
            bool res = false;
            object[] obj = UtilityService.GetFieldValueByPK("UserDetail", "WeChat", "WeChat", WeChat);
            if (obj != null && obj[0] != null)
            {
                res = true;
            }
            return res;
        }

        /// <summary>
        /// 员工手机激活 
        /// </summary>
        /// <param name="Mobile">员工手机号</param>
        /// <param name="WeChat">员工微信号</param>
        /// <returns></returns>        
        public static string Activate(string Mobile, string WeChat)
        {
            string msg = string.Empty;
            if (!IsActivate(WeChat))
            {
                object[] obj = UtilityService.GetFieldValueByPK("UserDetail", "UserID,IsLock", "Mobile", Mobile);
                if (obj != null && obj[0] != null)
                {
                    string UserID = obj[0].ToString();
                    string IsLock = obj[1].ToString();
                    if (IsLock == "1")
                    {
                        msg = "工号被锁定，请与管理员联系！";
                    }
                    else
                    {
                        if (UtilityService.UpdateFieldByPK("UserDetail", "WeChat", new object[] { WeChat }, "UserID", UserID) <= 0)
                        {
                            msg = "登录失败，请与管理员联系！";
                        }
                    }
                }
                else
                {
                    msg = "未找到对应工号，请与管理员联系！";
                }
            }
            return msg;
        }
    }
}